using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class ProviderDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetAllFromReceipt(string _Type, int _SpendID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_GetAllFromReceipt", dbConn);
            dbCmd.Parameters.AddWithValue("@Receipt_Type", _Type);
            dbCmd.Parameters.AddWithValue("@SpendID", _SpendID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _Provider_ID)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Provider_ID", _Provider_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(ProviderInfo _ProviderInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Provider_Code", _ProviderInfo.Provider_Code);
            dbCmd.Parameters.AddWithValue("@Provider_Name", _ProviderInfo.Provider_Name);
            dbCmd.Parameters.AddWithValue("@Provider_Address", _ProviderInfo.Provider_Address);
            dbCmd.Parameters.AddWithValue("@Provider_TaxNo", _ProviderInfo.Provider_TaxNo);
            dbCmd.Parameters.AddWithValue("@Provider_Phone", _ProviderInfo.Provider_Phone);
            dbCmd.Parameters.AddWithValue("@Provider_Contact", _ProviderInfo.Provider_Contact);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(ProviderInfo _ProviderInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_Update", dbConn);

            dbCmd.Parameters.AddWithValue("@Provider_ID", _ProviderInfo.Provider_ID);
            dbCmd.Parameters.AddWithValue("@Provider_Code", _ProviderInfo.Provider_Code);
            dbCmd.Parameters.AddWithValue("@Provider_Name", _ProviderInfo.Provider_Name);
            dbCmd.Parameters.AddWithValue("@Provider_Address", _ProviderInfo.Provider_Address);
            dbCmd.Parameters.AddWithValue("@Provider_TaxNo", _ProviderInfo.Provider_TaxNo);
            dbCmd.Parameters.AddWithValue("@Provider_Phone", _ProviderInfo.Provider_Phone);
            dbCmd.Parameters.AddWithValue("@Provider_Contact", _ProviderInfo.Provider_Contact);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static ProviderInfo GetInfo(int _Provider_ID)
        {
            ProviderInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Provider_ID", _Provider_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ProviderInfo();
                    retVal.Provider_ID = Convert.ToInt32(dr["Provider_ID"]);
                    retVal.Provider_Code = Convert.ToString(dr["Provider_Code"]);
                    retVal.Provider_Name = Convert.ToString(dr["Provider_Name"]);
                    retVal.Provider_Address = Convert.ToString(dr["Provider_Address"]);
                    retVal.Provider_TaxNo = Convert.ToString(dr["Provider_TaxNo"]);
                    retVal.Provider_Phone = Convert.ToString(dr["Provider_Phone"]);
                    retVal.Provider_Contact = Convert.ToString(dr["Provider_Contact"]);
                    retVal.Provider_CreateDate = Convert.ToDateTime(dr["Provider_CreateDate"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Using(int ProviderID)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Provider_ID", ProviderID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Exist(int ProviderID, string ProviderName)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Provider_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Provider_Name", ProviderName);
            dbCmd.Parameters.AddWithValue("@Provider_ID", ProviderID);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}

